/******************************************************************************
 *                                                                            *
 *               (c) 2008-2009 HutLab. All rights reserved.                   *
 *                        Eduardo Henrique Leite                              *
 *                                                                            *
 ******************************************************************************

 History:
 Created 04/01/2009

 */

package hutlab.sudoku.engine.math;

/**
 * Mathematical calculations for sudoku game.
 */
public class SudokuHelper {

    /**
     * Calculates the cell's column from index.
     */
    public static int calculateColumn(int index, int size){
        return index % size;
    }

    /**
     * Calculates the cell's row from index.
     */
    public static int calculateRow(int index, int size){
        return index / size;
    }

    /**
     * Calculates the cell's square from index. Revision needed.
     */
    public static int calculateSquare(int index, int size){
        int row = calculateRow(index, size);
        int col = calculateColumn(index, size);

        int square = -1;

        if (0 <= col && col < 3 && 0 <= row && row < 3){
            square = 0;
        }

        if (3 <= col && col < 6 && 0 <= row && row < 3){
            square = 1;
        }

        if (6 <= col && col < 9 && 0 <= row && row < 3){
            square = 2;
        }

        if (0 <= col && col < 3 && 3 <= row && row < 6){
            square = 3;
        }

        if (3 <= col && col < 6 && 3 <= row && row < 6){
            square = 4;
        }

        if (6 <= col && col < 9 && 3 <= row && row < 6){
            square = 5;
        }

        if (0 <= col && col < 3 && 6 <= row && row < 9){
            square = 6;
        }

        if (3 <= col && col < 6 && 6 <= row && row < 9){
            square = 7;
        }

        if (6 <= col && col < 9 && 6 <= row && row < 9){
            square = 8;
        }

        return square;
    }

    public static int calculateIndex(int row, int col, int size){
        return row * size + col;
    }
}
